Use processDeposit()
or processCheck()
.
class CheckingAccount { private String accountNumber; private String accountHolder; private int balance; . . . . } class CheckingAccountTester { public static void main( String[] args ) { CheckingAccount bobsAccount = new CheckingAccount( "999", "Bob", 100 ); System.out.println( bobsAccount.getBalance() ); bobsAccount.processDeposit( 200 ); System.out.println( bobsAccount.getBalance() ); } }
A class with private data provides access to that data through access methods. An access method uses the private data of its object and is visible to other classes. Some access methods alter data; others return a value but don't alter data.
The main()
program above
uses access methods of a CheckingAccount
object to change the object's data.
This is (usually) the correct way to use an object.
The methods of an object know best how to change its instance variables.
The idea of private
is to enforce this correct use.
Could an access method check for errors and change instance variables only if the data is correct?